//
//  JDProgressHUD.swift
//  readtool
//
//  Created by WanJiading on 16/6/13.
//  Copyright © 2016年 DAD. All rights reserved.
//

import UIKit
import SVProgressHUD

protocol JDProgressHUDDelegate {

    func jdProgressHUDDidTimeOut()

}

class JDProgressHUD: NSObject {

    static var delegate: JDProgressHUDDelegate?

	static var showingTime: Double = 30
    /// 人为设定时间
    static var customShowingTime: Double = 0
	static var showedTime: Double = 0
	static let timeSeperate: Double = 1
	static var timer1: Timer?


	
	/// message = nil, 只显示 等待，无任何文字
	internal class func loading(_ message: String?) {
		setDefaultConfig()
		if message == nil || message == "" {
			SVProgressHUD.show()
		} else {
			SVProgressHUD.show(withStatus: message!)
			//            SVProgressHUD.show(withStatus: message!)
		}
		calculateTime()
	}
	
	internal class func loading() {
		loading(nil)
	}
	
	internal class func loadingInView(_ superView: UIView) {
		SVProgressHUD.setContainerView(superView)
		SVProgressHUD.show()
	}

    internal class func loading(_ message: String?, _ duration: Double) {
        setDefaultConfig()
        if message == nil || message == "" {
            SVProgressHUD.show()
        } else {
            SVProgressHUD.show(withStatus: message!)
            //            SVProgressHUD.show(withStatus: message!)
        }
		calculateTime()
		customShowingTime = duration
    }
	
	internal class func success(_ message: String) {
		setDefaultConfig()
		SVProgressHUD.showSuccess(withStatus: message)
		//        SVProgressHUD.showSuccess(withStatus: message)
		calculateTime()
	}
	
	internal class func error(_ message: String) {
		setDefaultConfig()
		SVProgressHUD.showError(withStatus: message)
		//        SVProgressHUD.showError(withStatus: message)
		calculateTime()
	}
	
	internal class func info(_ message: String) {
		setDefaultConfig()
		SVProgressHUD.showInfo(withStatus: message)
		//        SVProgressHUD.showInfo(withStatus: message)
		calculateTime()
	}
	internal class func info(_ message: String, asf: String) {
		setDefaultConfig()
		SVProgressHUD.showInfo(withStatus: message)
		//        SVProgressHUD.showInfo(withStatus: message)
		calculateTime()
	}
	
	internal class func setDefaultConfig() {
		SVProgressHUD.setMinimumDismissTimeInterval(1.0)
		//        SVProgressHUD.setDefaultMaskType(SVProgressHUDMaskType.Black)
		
		SVProgressHUD.setDefaultMaskType(SVProgressHUDMaskType.clear)
		SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.dark)
		SVProgressHUD.setDefaultAnimationType(SVProgressHUDAnimationType.native)
		
		//        SVProgressHUD.setDefaultMaskType(SVProgressHUDMaskType.black)
	}
	
	internal class func dismiss() {
		SVProgressHUD.dismiss()
		
		reset()
	}
	
	internal class func calculateTime() {
		reset()
		timer1 = Timer(timeInterval: timeSeperate, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
		RunLoop.main.add(timer1!, forMode: RunLoopMode.commonModes)
	}
	@objc internal class func updateTime() {
        showedTime += timeSeperate
        DebugLog("\(showedTime)")

        var maxShowTime = showingTime
        if customShowingTime > 0 {
            maxShowTime = customShowingTime
        }

		if showedTime == maxShowTime {
			timer1?.invalidate()
			timer1 = nil

            reset()

			self.dismiss()

//			JDToast.show(ToastConst.HUD_Timeout_Request)

            if delegate != nil {
                delegate!.jdProgressHUDDidTimeOut()
            }

		}
	}
	
	fileprivate class func invalidateTimer() {
		if self.timer1 != nil && self.timer1!.isValid {
			self.timer1!.invalidate()
			self.timer1 = nil
		}
	}
	
	fileprivate class func reset() {
	    invalidateTimer()
		showedTime = 0
        customShowingTime = 0
	}
	
}

//extension JDProgressHUD {
//    enum Error {
//        case Custom(String)
//        case Network
//
//        var show: Void {
//
//            var state: String?
//
//            switch self {
//            case .Custom(let string): state = string
//            case .Network: state = "无网络"
//            }
//            SVProgressHUD.setDefaultMaskType(.Black)
//            SVProgressHUD.showErrorWithStatus(state)
//            dispatch_after(1.5) { SVProgressHUD.dismiss() }
//        }
//    }
//
//    enum Success {
//        case Custom(String)
//
//        var show: Void {
//
//            var state: String?
//
//            switch self {
//            case .Custom(let string): state = string
//            }
//            SVProgressHUD.showSuccessWithStatus(state)
//            dispatch_after(1.5) { SVProgressHUD.dismiss() }
//        }
//    }
//
//    enum Loading {
//        case Custom(String)
//
//        var show: Void {
//
//            var state: String?
//
//            switch self {
//            case .Custom(let string): state = string
//            }
//            SVProgressHUD.showWithStatus(state)
//        }
//    }
//
//    enum Info {
//        case Custom(String)
//
//        var show: Void {
//
//            var state: String?
//
//            switch self {
//            case .Custom(let string): state = string
//            }
//            SVProgressHUD.showInfoWithStatus(state)
//            dispatch_after(1.5) { SVProgressHUD.dismiss() }
//        }
//    }
//    class var dismiss: Void {
//        SVProgressHUD.dismiss()
//    }
//}
